package com.uit.httt04.duongvanlam.facedetection.utils;

import static com.googlecode.javacv.cpp.opencv_core.*;

public class Matrix
{
	static public String TAG = "Matrix";

	static public double DistanceByCol(CvMat A, CvMat B) throws Exception
	{
		if (A != null && B != null && A.cols() == B.cols() && A.rows() == B.rows() && A.channels() == 1 && B.channels() == 1)
		{
			int cols = A.cols();
			int rows = A.rows();
			int type = A.type();

			CvMat subA = cvCreateMat(rows, 1, type);
			CvMat subB = cvCreateMat(rows, 1, type);

			double result = (double) 0.0;

			for (int i = 0; i < cols; i++)
			{
				cvGetCol(A, subA, i);
				cvGetCol(B, subB, i);
				result = result + cvNorm(subA, subB, NORM_L2);
			}

			cvReleaseMat(subA);
			cvReleaseMat(subB);

			return result;
		}
		else
			throw new Exception("Difference size");
	}

	/*static public double DistanceRow(CvMat A, CvMat B) throws Exception
	{
		if (A != null && B != null && A.cols() == B.cols() && A.rows() == B.rows() && A.channels() == 1 && B.channels() == 1)
		{
			double result = (double) 0.0;
			double temp;
			for (int i = 0; i < A.rows(); i++)
			{
				temp = (double) 0.0;
				//
				for (int j = 0; j < A.cols(); j++)
				{
					temp = temp + Math.pow((A.get(i, j) - B.get(i, j)), 2);
				}
				//
				result = result + Math.sqrt(temp);
			}

			return result;
		}
		else
			throw new Exception("Difference size");
	}*/

	static public double Distance(CvMat A, CvMat B) throws Exception
	{
		if (A != null && B != null && A.cols() == B.cols() && A.rows() == B.rows() && A.channels() == 1 && B.channels() == 1)
		{
			double result = (double) 0.0;
			/*for (int i = 0; i < A.rows(); i++)
				for (int j = 0; j < A.cols(); j++)
				{
					result = result + Math.pow((A.get(i, j) - B.get(i, j)), 2);
				}

			return Math.sqrt(result);*/
			result = cvNorm(A, B, NORM_L2);
			return result;
		}
		else
			throw new Exception("Difference size");
	}

	/*static public void PrintMat(CvMat A, String MatrixName)
	{
		Log.i(TAG, "PrintMat: " + MatrixName);
		String row;
		for (int i = 0; i < A.rows(); i++)
		{
			row = "";
			for (int j = 0; j < A.cols(); j++)
			{
				row = row + A.get(i, j) + " ";
			}
			Log.i(TAG, row);
		}
	}*/
}
